package com.cuit.handler.risk;

import com.cuit.bean.command.CmdResultCode;
import com.cuit.bean.command.RbCmd;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.api.set.primitive.MutableLongSet;
import thirdpart.order.CmdType;


@Log4j2
@RequiredArgsConstructor
public class ExistRiskHandler extends BaseHandler {

    //用户ID的集合
    @NonNull
    private MutableLongSet uidSet;

    //股票代码的集合
    @NonNull
    private MutableIntSet codeSet;


    //发布行情event
    //新委托event
    //撤单event
    @Override
    public void onEvent(RbCmd cmd, long l, boolean b) throws Exception {
        //系统内部指令:行情发布,不进行前置风控判断
        if (cmd.command == CmdType.HQ_PUB) {
            return;
        }

        if (cmd.command == CmdType.NEW_ORDER || cmd.command == CmdType.CANCEL_ORDER) {
            //1.用户是否存在
            if (!uidSet.contains(cmd.uid)) {
                log.error("illegal uid[{}] exist", cmd.uid);
                cmd.resultCode = CmdResultCode.RISK_INVALID_USER;
                return;
            }
            //2.股票代码是否合法
            if (!codeSet.contains(cmd.code)) {
                log.error("illegal code[{}] exist", cmd.code);
                cmd.resultCode = CmdResultCode.RISK_INVALID_CODE;
                return;
            }
        }
    }
}
